package com.cy.ppj.web.admin.product;

import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.product.ProductTypeTagAO;
import com.cy.ppj.model.domian.product.ProductType;
import com.cy.ppj.service.product.ProductService;
import com.cy.ppj.service.product.TagService;
import com.cy.ppj.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @description: 标签管理
 * @author: Ocean
 * @create: 2018-07-17 18:49
 **/
@Slf4j
@RestController
@RequestMapping("/admin/product/type")
@Api(tags = "后台-产品类型管理")
public class AdminProductTypeController extends BaseController {

    @Autowired
    private ProductService productService;
    @Autowired
    private TagService tagService;

    @ApiOperation("新增产品类型")
    @PostMapping("/add")
    public Result add(@ApiParam(value = "类型名称", required = true) @RequestParam String name) {
        productService.addType(name);
        return ok();
    }

    @ApiOperation("编辑产品类型")
    @PostMapping("/edit")
    public Result edit(@Validated ProductType productType, BindingResult bindingResult) {
        this.checkError(bindingResult);
        productService.updateType(productType);
        return ok();
    }

    @ApiOperation("全部产品类型")
    @GetMapping("/all/types")
    public Result allTypes() {
        return ok("查询成功", productService.allTypesInCache());
    }

    @ApiOperation("产品类型添加标签")
    @PostMapping("/add/tags")
    public Result addTags(@Validated ProductTypeTagAO productTypeTagAO, BindingResult bindingResult) {
        this.checkError(bindingResult);
        tagService.addTypeTags(productTypeTagAO);
        return ok();
    }

}
